refactor: consolidate atomic writes and locking to use fileutil primitives#113
Open
xzq-xu wants to merge 1 commit intoHKUDS:mainfrom
Open
refactor: consolidate atomic writes and locking to use fileutil primitives#113xzq-xu wants to merge 1 commit intoHKUDS:mainfrom
xzq-xu wants to merge 1 commit intoHKUDS:mainfrom
Conversation
4581d6a to
af0a2ec
Compare
- store/file.py: replace 22-line _write_lock with file_locked() and replace _save_unlocked with atomic_write_text(), removing duplicated platform-specific locking code (sys, fcntl/msvcrt, contextmanager, tempfile imports all removed). - routing_policy.py: wrap read-modify-write cycles in file_locked() to prevent concurrent processes from losing state updates; replace hand-rolled mkstemp+replace in _save_state with atomic_write_text(). - router.py: replace json.loads(model_dump_json()) with model_dump() to eliminate unnecessary serialization round-trip. Made-with: Cursor
af0a2ec to
c2fdee1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_save_stateperformed atomic writes but lacked advisory locking. Multiple processes (e.g. several agents runninginbox watch) could interleaveread_state→ mutate →_save_stateand silently lose updates. All read-modify-write cycles (decide,flush_due,record_dispatch_result) are now wrapped infile_locked().FileTaskStore._write_lockreimplemented the full 22-line platform-specificfcntl/msvcrtlocking pattern already provided byfileutil.file_locked(). Replaced with a one-liner, removingsys,contextmanager,tempfile, and platform imports._save_unlockedreimplementedmkstemp+os.replace+ cleanup already provided byfileutil.atomic_write_text(). Replaced with a one-liner.json.loads(message.model_dump_json(...))serialized to string then immediately deserialized. Replaced with directmessage.model_dump(...).Net result: -34 lines, one concurrency bug fixed, three modules now consistently use the project's own
fileutilprimitives.Test plan
.tasks.lockin tasks root)Made with Cursor